package logic

import (
	"context"
	"fmt"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *personrpc.LonginRequest) (*personrpc.LonginResponse, error) {
	// todo: add your logic here and delete this line
	var findper mysql.Person
	mysql.DB.Find(&findper)
	if findper.ID == 0 {
		return nil, fmt.Errorf("用户还未注册，请先去注册")
	}
	if findper.Password != in.Password {
		return nil, fmt.Errorf("密码不正确，请重新输入")
	}
	return &personrpc.LonginResponse{Id: int64(findper.ID)}, nil
}
